<template>
  <section class="c-cont">
    <flexbox class="c-header">
      <i :class="['wk', `wk-${type}`]"/>
      <div class="c-name">{{ typeName }}</div>
    </flexbox>
    <div class="c-body">
      <flexbox wrap="wrap">
        <flexbox v-for="(item, index) in data" :key="index" class="c-item">
          <div class="c-item-name">{{ getItemName(item) }}</div>
          <div class="c-item-close" @click="deleteItem(item, index)">×
          </div>
        </flexbox>
      </flexbox>
    </div>
  </section>
</template>

<script setup>
/**
 * 名称字段
 * customerName
 * name
 * businessName
 * name
 */

defineOptions({
  // 添加相关业务信息展示
  name: 'AddRelateList',
})

const properties = defineProps({
  data: Array,
  // 业务关键字 customer contacts business contract
  type: {
    type: [String, Number],
    require: true
  },
  label: String
})

const emits = defineEmits(['delete'])

const typeName = computed(() => {
  if (properties.label) {
    return properties.label
  }

  return {
    customer: '客户',
    contacts: '联系人',
    business: '商机',
    contract: '合同'
  }[properties.type]
})

function getItemName(data) {
  const key = {
    customer: 'customerName',
    contacts: 'name',
    business: 'businessName',
    contract: 'name'
  }[properties.type]
  return data[key] || data.name
}

function deleteItem(item, index) {
  emits('delete', item, index, properties.type)
}

</script>

<style lang="scss" scoped>
/** CRM  */
.c-cont {
  padding: 0 10px;
  margin: 0 10px 15px;
  border: 1px dashed #dfdfdf;

  .c-header {
    padding: 8px 0 15px;
    font-size: 13px;
    color: #333;

    .wk {
      font-size: 14px;
      color: #333;
      margin-right: 8px;
    }

    .c-name {
      font-weight: 600;
    }
  }

  .c-body {
    margin-bottom: 10px;

    .c-item {
      height: 24px;
      border-radius: 12px;
      padding: 0 8px;
      margin: 0 5px 5px 0;
      background-color: $xr-color-primary;
      color: white;
      width: auto;
      cursor: pointer;

      .c-item-name {
        font-size: 12px;
      }

      .c-item-close {
        padding-left: 5px;
        font-size: 17px;
      }
    }
  }
}
</style>
